package com.websocket;

import com.websocket.config.DefaultWebSocketConfigurer;
import com.websocket.converter.MessageConverter;
import com.websocket.event.EventListenerConfig;
import com.websocket.event.EventPublisher;
import com.websocket.event.EventListenerRegistry;
import com.websocket.hander.InterceptorRegistry;
import com.websocket.handshake.HandshakeRegistry;
import org.java_websocket.handshake.ClientHandshake;
import org.springframework.context.ApplicationEventPublisher;

import java.security.Principal;
import java.util.function.Function;

public class WebSocketConfig {

    public final HandshakeRegistry handshakeRegistry = new HandshakeRegistry();
    public final InterceptorRegistry interceptorRegistry = new InterceptorRegistry();
    public final EventListenerConfig eventListenerConfig = new EventListenerConfig();

    public final MessageConverter converter;
    public final Function<ClientHandshake, Principal> determineUser;
    public final EventPublisher eventPublisher;

    public WebSocketConfig(DefaultWebSocketConfigurer configurer, ApplicationEventPublisher eventPublisher) {
        this.determineUser = configurer::determineUser;
        configurer.registerHandshake(handshakeRegistry);
        configurer.registryHandler(interceptorRegistry);
        configurer.registryEventListener(new EventListenerRegistry(eventListenerConfig));
        this.converter = new MessageConverter(configurer.setMessageConverter());
        this.eventPublisher = new EventPublisher(eventListenerConfig, eventPublisher);
    }


}
